package com.codename1.samples;


import com.codename1.components.SpanLabel;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.CommonProgressAnimations;
import com.codename1.ui.TextArea;
import com.codename1.ui.animations.CommonTransitions;
import com.codename1.ui.util.UITimer;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class ProgressAnimationsSample {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        Form hi = new Form("Hi World", BoxLayout.y());
        hi.add(new Label("Hi World"));
        hi.add(new CommonProgressAnimations.CircleProgress());
        hi.add(new CommonProgressAnimations.LoadingTextAnimation());
        
        Label labelThatIsLoading = new Label("Loading...");
        
        hi.add(labelThatIsLoading);
        CommonProgressAnimations.CircleProgress.markComponentLoading(labelThatIsLoading);
        UITimer.timer(2000, false, hi, ()-> {
            labelThatIsLoading.setText("Found 248 results");
            CommonProgressAnimations.CircleProgress.markComponentReady(labelThatIsLoading);
        });
        
        Label anotherLabelThatIsLoading = new Label("Loading...");
        
        hi.add(anotherLabelThatIsLoading);
        CommonProgressAnimations.CircleProgress.markComponentLoading(anotherLabelThatIsLoading);
        UITimer.timer(4000, false, hi, ()-> {
            labelThatIsLoading.setText("Found 512 results");
            CommonProgressAnimations.CircleProgress.markComponentReady(anotherLabelThatIsLoading, CommonTransitions.createFade(300));
        });
        
        TextArea someText = new TextArea("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
        someText.setGrowByContent(true);
        someText.setRows(4);
        someText.setColumns(40);
        someText.setPreferredW(Display.getInstance().getDisplayWidth());
        hi.add(someText);
        CommonProgressAnimations.LoadingTextAnimation.markComponentLoading(someText).cols(40).rows(5);
        UITimer.timer(6000, false, hi, ()-> {
            CommonProgressAnimations.CircleProgress.markComponentReady(someText, CommonTransitions.createFade(300));
        });
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
